//=============================================================================
/**
 *
 *	@file		pkthln_break.c
 *	@brief		ポケスロン競技：かわらわり
 *	@author		hosaka genya
 *	@data		2008.11.06
 *
 */
//=============================================================================
#include "common.h"
#include "strbuf_family.h"
#include "system/wipe.h"
#include "system/clact_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/gra_tool.h"
#include "gflib/touchpanel.h"
#include "poketool/pokeicon.h"

#include "../pkthln_clact.h"
#include "../pkthln_act_def.h"

#include "../../pkthln_comm_pack.h"
#include "../../pkthln_obj_common.h"
#include "../../poke_clact.h"
#include "../../poke_clact_move.h"
#include "../../pkthln_poke_act.h"
#include "../../cmn_res.h"
#include "../evt_cats.h"
#include "../event_se_def.h"

//成績作成用
#include "system/msgdata_util.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pokethlon.h"

#include "break_common.h"
#include "break_kawara.h"
#include "break_poke.h"
#include "break_ai.h"
#include "pkthln_break.h"

#include "pkthev_break.naix"
#include "break_obj_main_NANR_LBLDEFS.h"
#include "break_obj_sub_NANR_LBLDEFS.h"

#ifdef _NITRO
		// 構造体が想定のサイズとなっているかチェック
		SDK_COMPILER_ASSERT(sizeof(BREAK_COMM_POKE_PARAM) == 4);
		SDK_COMPILER_ASSERT(sizeof(BREAK_CHILD_DATA) == 36);
		SDK_COMPILER_ASSERT(sizeof(BREAK_PARENT_DATA) == 36);
#endif

#ifdef PM_DEBUG
		extern DEBUG_PKTHLN_WORK DebugPkthlnWk;
#endif

//=============================================================================
/**
 *								定数定義
 */
//=============================================================================

// -- 透明色 --
#define COL_NL (GF_PRINTCOLOR_MAKE(0, 0, 0))

#define	COL_N_WHITE	( GF_PRINTCOLOR_MAKE( 15, 14, 0 ) )		// フォントカラー：白

//--------------------------------------------------------------
///	汎用定数
//==============================================================
enum
{
	POKE_ACT_NUM = (ENTRY_MEMBER_NUM_MAX-1) * ENTRY_MAX,
	TIME_FIGRE = 2,
	SCORE_FIGRE = 3,
	SCORE_FONT_POS_X = FX32_CONST(1*8),
	SCORE_FONT_OFS_X = FX32_CONST(2*8),
	SCORE_FONT_POS_Y = FX32_CONST(22*8),

	COMM_KAWARA_MAX = 20,	///< 通信用かわらの数
	COMM_KAWARA_BACK_MAX = 5,
	COMM_KAWARA_HIGHT = 4,
	COMM_KAWARA_BACK_POS_DIFF_X = 6,
	COMM_KAWARA_BACK_POS_DIFF_Y = -20,
};

enum
{
	CAP_ID_M_ICON_TIMER = 0,
	CAP_ID_M_BTN_CHANGE,
	CAP_ID_MAX,
};

//--------------------------------------------------------------
///	BG用定数
//==============================================================
enum {
	BG_FRM_M_BASE = GF_BGL_FRAME0_M,
	BG_FRM_M_TEXT = GF_BGL_FRAME1_M,
	BG_FRM_M_RSLT = GF_BGL_FRAME2_M,
	BG_FRM_S_BASE = GF_BGL_FRAME0_S,
	BG_FRM_S_WIN =  GF_BGL_FRAME1_S,
	BG_FRM_S_TEXT = GF_BGL_FRAME2_S,

	BG_FRM_M_RSLT_MASK = GX_PLANEMASK_BG2,
};

#define BG_USE_NUM (6)
static const c_BG_UseList[ BG_USE_NUM ] =
{
	BG_FRM_M_BASE,
	BG_FRM_M_TEXT,
	BG_FRM_M_RSLT,
	BG_FRM_S_BASE,
	BG_FRM_S_WIN,
	BG_FRM_S_TEXT,
};

enum {
	MAIN_FONT_PAL = 15,
	SUB_FONT_PAL = 15,

	BMPWIN_ID_M_SCORE_CAP = 0,
	BMPWIN_ID_M_TIME,
	BMPWIN_ID_M_RECORD,
	BMPWIN_ID_S_POKENICK,
	BMPWIN_ID_MAX,
};

#define BMP_STRING_H (2)
#define BMP_M_SCORE_CAP_W (3)
#define BMP_M_TIME_W (7)

#define WIN_REC_PX		(0)
#define WIN_REC_PY		(20)
#define WIN_REC_SX		(32)
#define WIN_REC_SY		(2)

#define BMP_M_SCORE_CAP_CGX (1)
#define BMP_M_TIME_CGX ( BMP_M_SCORE_CAP_CGX + BMP_M_SCORE_CAP_W * BMP_STRING_H )
#define BMP_M_RECORD_CGX ( BMP_M_TIME_CGX + BMP_M_TIME_W * BMP_STRING_H )

// LCD Sub
#define BMP_S_POKENICK_W (15)
#define BMP_S_POKENICK_CGX (1)

static const BMPWIN_DAT c_BmpWinList[ BMPWIN_ID_MAX ] =
{
	// --- main ---
	{ BG_FRM_M_TEXT, 6, 22,		BMP_M_SCORE_CAP_W,	BMP_STRING_H, MAIN_FONT_PAL, BMP_M_SCORE_CAP_CGX },
	{ BG_FRM_M_TEXT, 25, 22,	BMP_M_TIME_W,		BMP_STRING_H, MAIN_FONT_PAL, BMP_M_TIME_CGX },
	{ BG_FRM_M_RSLT, WIN_REC_PX, WIN_REC_PY, WIN_REC_SX, WIN_REC_SY, MAIN_FONT_PAL,  BMP_M_RECORD_CGX },
	{ BG_FRM_S_TEXT, 0, 22,		BMP_S_POKENICK_W,	BMP_STRING_H, SUB_FONT_PAL,  BMP_S_POKENICK_CGX },	// BMPWIN_ID_S_POKENICK
};


//=============================================================================
/**
 *								構造体定義
 */
//=============================================================================

//--------------------------------------------------------------
///	上画面情報表示用ワーク(1チーム)
//==============================================================
typedef struct {
	u8 PokeIdxPre;
	CATS_ACT_PTR capPoke;
	CATS_ACT_PTR capBall;
	CATS_ACT_PTR capKawara[ COMM_KAWARA_MAX ];
	CATS_ACT_PTR capKawaraBack[ COMM_KAWARA_BACK_MAX ];
} COMM_DISP;


//--------------------------------------------------------------
///	上画面情報表示用ワーク(通信データから参照)
//==============================================================
typedef struct COMM_DISP_CNT_tag {
	ARCHANDLE* pArcPokeIcon;
	COMM_DISP team[ ENTRY_MEMBER_NUM_MAX ];
	u16 PlttOfsPokeIcon;
} COMM_DISP_CNT;


//--------------------------------------------------------------
///	タッチ交代判定用ワーク
//==============================================================
typedef struct {
	u32 Count : 8;
	u32 PosYTouch : 8;
	u32 PosYRelease : 8;
	u32 bTouch : 1;
	u32 DummyBit : 7;
} TP_CHANGE_WORK;

//--------------------------------------------------------------
///	メインワーク
//==============================================================
typedef struct PKTHLN_BREAK_tag {
	// -- in --
	HEAPID	HeapID;
	// -- gf system pointer --
	GF_BGL_INI*		bgl;
	CATS_SYS_PTR	csp;
	CATS_RES_PTR	crp;
	// -- pokethlon system pointer --
	POKE_ACT_CNT_PTR		PokeActCntPtr;
	CMN_RES_CNT_PTR			CmnResCntPtr;
	// -- private --
	STRBUF*	NickName[ ENTRY_MAX ]; ///< ポケモンニックネーム
	CATS_ACT_PTR			cap[ CAP_ID_MAX ];
	CLACT_WORK_PTR			actScore[ SCORE_FIGRE ];
	CLACT_WORK_PTR			actTime[ TIME_FIGRE ];
	KAWARA_CNT_PTR			cntKawara;
	COMM_POKE_CNT_PTR		cntCommPoke;
	BREAK_AI_CNT_PTR		cntAI;
	BREAK_POKE_PTR			mainPoke;

	COMM_DISP_CNT			cntCommDisp;

	GF_BGL_BMPWIN			win[BMPWIN_ID_MAX];
	MSGDATA_MANAGER*		MsgMan;
	WORDSET*				WordSet;
	s32						GameTimer;
	TP_CHANGE_WORK			wkTpChg;
	// 画面揺らし処理
	TCB_PTR					tcbShake;
	s8						ShakeVal;
	u8						ShakeDecVal;
	u8						ShakeVec;
	u8						DummyBit;
} PKTHLN_BREAK;

//=============================================================================
/**
 *							データテーブル
 */
//=============================================================================
static const g_kawara_anmseq[ ENTRY_MEMBER_NUM_MAX ][3] =
{
	{ NANR_break_obj_sub_red_0,		NANR_break_obj_sub_red_1,		NANR_break_obj_sub_red_2	},
	{ NANR_break_obj_sub_blue_0,	NANR_break_obj_sub_blue_1,		NANR_break_obj_sub_blue_2	},
	{ NANR_break_obj_sub_yellow_0,	NANR_break_obj_sub_yellow_1,	NANR_break_obj_sub_yellow_2	},
	{ NANR_break_obj_sub_green_0,	NANR_break_obj_sub_green_1,		NANR_break_obj_sub_green_2	},
};

//=============================================================================
/**
 *							プロトタイプ宣言
 */
//=============================================================================
// -- prototype --
static void SetVramBank(void);
static void OBJ_Create( PKTHLN_BREAK_PTR ptr );
static void OBJ_Delete( PKTHLN_BREAK_PTR ptr );
static void OBJ_CreateActor( PKTHLN_BREAK_PTR ptr );
static void OBJ_DeleteActor( PKTHLN_BREAK_PTR ptr );
static void OBJ_LoadRes( PKTHLN_BREAK_PTR ptr );
static void OBJ_LoadResPokegra( CATS_SYS_PTR csp, CATS_RES_PTR crp );
static void OBJ_ScoreFontSet( PKTHLN_BREAK_PTR ptr );
static void OBJ_Finish( PKTHLN_BREAK_PTR ptr );
static void BG_Init( GF_BGL_INI* bgl, HEAPID HeapID );
static void BG_Delete( PKTHLN_BREAK_PTR ptr );
static void BG_LoadRes( PKTHLN_BREAK_PTR ptr );
static void BG_BmpWinCreate( PKTHLN_PTR work );
static void BG_PrintFlash( PKTHLN_BREAK_PTR ptr );
static void BG_PrintFirst( PKTHLN_BREAK_PTR ptr );
static void BMPWIN_Print( GF_BGL_BMPWIN* win, WORDSET* WordSet, MSGDATA_MANAGER* MsgMan, u32 msgIdx, u32 HeapID );
static void CommDispCnt_Init( PKTHLN_PTR work );
static void CommDispCnt_Delete( PKTHLN_BREAK_PTR ptr );
static void CommDispCnt_Set( PKTHLN_PTR work );
static void CommDispCnt_SetPokeIcon( PKTHLN_PTR work, u8 NetID, u8 PokeIdx );
static void ChangeCheck( PKTHLN_BREAK_PTR ptr, BOOL b_kawara_touch );
static void COMM_SendData( PKTHLN_PTR work );
static void COMM_Main( PKTHLN_PTR work );
static void SetRecordStr( PKTHLN_PTR work, const int inPoint );
static void TCB_ShakeTask( TCB_PTR tcb, void* work );
static void StartShakeTask( PKTHLN_BREAK_PTR ptr, u8 ShakeVal, u8 ShakeDecVal );

//=============================================================================
/**
 *								外部公開関数
 */
//=============================================================================

// --- Event Func ---
static BOOL BREAK_Func_Phase1(PKTHLN_PTR work, u8 *seq);
static BOOL BREAK_Func_Phase2(PKTHLN_PTR work, u8 *seq);
static BOOL BREAK_Func_Phase3(PKTHLN_PTR work, u8 *seq);

PKTHLN_FUNC BreakFuncTbl[] = {
	BREAK_Func_Phase1,
	BREAK_Func_Phase2,
	BREAK_Func_Phase3,
};

enum {
	BREAK_SEQ_PHASE1,
	BREAK_SEQ_PHASE2,
	BREAK_SEQ_PHASE3,
};

//-----------------------------------------------------------------------------
/**
 *	@brief	初期化シーケンス関数
 *
 *	@param	work
 *	@param	*seq
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL BREAK_Func_Phase1(PKTHLN_PTR work, u8 *seq)
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);

	switch( *seq )
	{
		case 0 :
			// かわら出現
			KawaraCnt_Proc( ptr->cntKawara );

			if( KawaraCnt_GetState( ptr->cntKawara ) == KC_STATE_NORMAL )
			{
				// ペン表示
				{
					PEN_POS pen_pos = { KAWARA_STD_POS_X, KAWARA_STD_POS_Y - 9 * KAWARA_DOT_HIGHT, };
					CLACT_SET_PTR clact_set_ptr = CATS_GetClactSetPtr( ptr->crp );

					PTFrame_SetPenByClSet( work, 0, ptr->CmnResCntPtr, clact_set_ptr, NNS_G2D_VRAM_TYPE_2DMAIN,	1, &pen_pos );
				}

				(*seq)++;
			}
			break;

		case 1 :
			// カウントダウン
			if ( PTFrame_CountDown(work) )
			{
				// 全ポケモンアニメ開始
				PokeAct_AllAnmStop( ptr->PokeActCntPtr, FALSE );
				PTFrame_SetNextFuncTblIdx(work, BREAK_SEQ_PHASE2);
				HOSAKA_PRINT("メインシーケンスへ\n");
			}
			break;

		default : GF_ASSERT(0);
	}

	return FALSE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	メインシーケンス関数
 *
 *	@param	work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL MainProc(PKTHLN_PTR work)
{
	BOOL b_kawara_touch;
	KC_STATE kcState;
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);

#ifdef PM_DEBUG
	// Xで強制終了
	if( sys.trg & PAD_BUTTON_X ){ ptr->GameTimer = 0; }
#endif


	// 終了判定
	{
		COMM_EVENT_COMMON_DATA* data;
		BREAK_PARENT_DATA* parent;

		data = PTFrame_GetEvtComCmnData(work);
		parent = PKTHLN_CommPack_GetData(&data->ReciveShareData);

		if( parent->bGameEnd == 1 )
		{
			// 終了！
			SetRecordStr( work, KawaraCnt_GetBreakCount( ptr->cntKawara ) );
			OBJ_Finish( ptr );
			BreakPoke_SetAutoAnime( ptr->mainPoke, FALSE );
			CommPokeCnt_Finish( ptr->cntCommPoke );
			KawaraCnt_SetEnd( ptr->cntKawara );
			PokeAct_AllAnmStop( ptr->PokeActCntPtr, TRUE );

			return TRUE;
		}
	}

	// AP:タッチ回数
	if( GF_TP_GetTrg() )
	{
		PTFrame_AddActionPoint( work, PTFrameGetCurrentID(work), ENTRY_MAX, ADD_TYPE_TOUCH, 1 );
	}

	// BG更新
	BG_PrintFlash( ptr );

	// かわら処理
	KawaraCnt_Proc( ptr->cntKawara );

	kcState = KawaraCnt_GetState( ptr->cntKawara );

	// 自分のポケモン主処理
	BreakPoke_Proc( ptr->mainPoke, (kcState==KC_STATE_MAKE) );

	// AI処理(親のみ)
	if ( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
	{
#ifdef PM_DEBUG
		if( DebugPkthlnWk.AI_Stop == 0 )
#endif
		{

			BreakAICnt_Proc( ptr->cntAI );
		}
	}

	// 他のポケモンの主処理
	CommPokeCnt_Proc( ptr->cntCommPoke );

	// kawara当たり判定
	b_kawara_touch = KawaraCnt_CheckHit( ptr->cntKawara );

	// 交代チェック
	ChangeCheck( ptr, b_kawara_touch );

	if( BreakPoke_IsAttackEnable( ptr->mainPoke ) && kcState == KC_STATE_NORMAL )
	{
		// かわらヒット処理
		if( b_kawara_touch )
		{
			const BREAK_POKE_PARAM* prm = BreakPoke_GetActiveParam( ptr->mainPoke );
			BOOL bCritical;
			BOOL bHighTension;
			BOOL bBreakAllFlag;
			u8 PokeIdx;
			u16 Damage;
			u16 BreakNum;

			bHighTension = BreakPoke_GetHighTensionFlag( ptr->mainPoke );
			bCritical = KawaraCnt_CheckCritical( ptr->cntKawara, prm->Tec );
			PokeIdx = BreakPoke_GetPokeIdx( ptr->mainPoke );
			Damage = prm->Pow;
			BreakNum = KawaraCnt_Damage( ptr->cntKawara, PokeIdx, Damage, bCritical, bHighTension, &bBreakAllFlag );

			// 攻撃処理
			BreakPoke_Attack( ptr->mainPoke, bCritical, BreakNum );

			// スコアフォントセット
			OBJ_ScoreFontSet( ptr );

			//----------------------------------------------------------------
			// 画面をゆらす
			//----------------------------------------------------------------
			if( bCritical )
			{
				StartShakeTask( ptr, SCR_DEF_OFS, 2 );
			}
			else if( bBreakAllFlag )
			{
				StartShakeTask( ptr, 4, 1 );
			}
			else if( BreakPoke_GetHighTensionFlag( ptr->mainPoke ) )
			{
				StartShakeTask( ptr, 4, 1 );
			}
		}
	}

	// AI
#ifdef PM_DEBUG
	if( DebugPkthlnWk.TimeInfinity == 0 )
#endif
	{
		if( ptr->GameTimer > 0 )
		{
			ptr->GameTimer--;
		}
	}

	// ラスト3秒前 表記更新
	PTFrame_DispLastCount( work, ptr->GameTimer );

	// 終了判定
	if( ptr->GameTimer == 0 )
	{
		if( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
		{
			// 終了フラグを送信
			COMM_EVENT_COMMON_DATA* data;
			BREAK_PARENT_DATA* parent_send;

			data = PTFrame_GetEvtComCmnData(work);
			parent_send = PKTHLN_CommPack_GetData(&data->SendShareData);

			parent_send->bGameEnd = 1;
		}
	}

	return FALSE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	メインシーケンス関数
 *
 *	@param	work
 *	@param	*seq
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL BREAK_Func_Phase2(PKTHLN_PTR work, u8 *seq)
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);

	switch( *seq )
	{
		case 0:
			// 通信処理
			COMM_Main(work);
			// 通信状況表示
			CommDispCnt_Set(work);
			if( MainProc(work) )
			{
				(*seq)++;
			}
			break;

		case 1:
			// リザルト表示
			if( PTFrame_DispEnd(work) )
			{
				// TCB終了待ち
				if( ptr->tcbShake == NULL )
				{
					//フェードアウト
					WIPE_SYS_Start(	WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT,
								WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK,
								WIPE_DEF_DIV, WIPE_DEF_SYNC, ptr->HeapID );

					PTFrame_SetNextFuncTblIdx(work, BREAK_SEQ_PHASE3);
				}
			}
			break;
	}

	return FALSE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	結果シーケンス関数
 *
 *	@param	work		ポケスロンポインタ
 *	@param	*seq		シーケンサ
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL BREAK_Func_Phase3(PKTHLN_PTR work, u8 *seq)
{
	PKTHLN_BREAK_PTR ptr =  PTFrame_GetEventWorkPtr(work);

	if( WIPE_SYS_EndCheck() )
	{
		int netID = PTFrameGetCurrentID(work);

		PTFrame_SetEvtResultOrg( work, netID, KawaraCnt_GetBreakCount( ptr->cntKawara ) );

		// 親はNPCの結果をセット
		if( netID == COMM_PARENT_ID )
		{
			int i;
			int PlayerNum = PTFrame_GetPlayerNum(work);

			for(i=PlayerNum; i<ENTRY_MEMBER_NUM_MAX; i++ )
			{
				COMM_EVENT_COMMON_DATA* data;
				BREAK_CHILD_DATA* child_rec;

				data = PTFrame_GetEvtComCmnData(work);
				child_rec = PKTHLN_CommPack_GetData(&data->ReciveData[i]);

				PTFrame_SetEvtResultOrg( work, i, child_rec->BreakCount );
			}
		}

		HOSAKA_PRINT("Next To ResultFunc \n");
		return TRUE;
	}

	return FALSE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	初期化関数
 *
 *	@param	PKTHLN_PTR work
 *	@param	pParam
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
PTH_RC PE_BREAK_InitFunc( PKTHLN_PTR work, void* pParam )
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);
	u8 seq = PTFrame_GetSubSeq(work);

	switch( seq )
	{
		case 0 :
			// ヒープ生成
			sys_CreateHeap( HEAPID_POKETHLON, HEAPID_PT_BREAK, BREAK_HEAP_SIZE );

			// BlankFunc Reset
			sys_VBlankFuncChange( NULL, NULL );	// VBlankセット
			sys_HBlankIntrSet( NULL,NULL );		// HBlankセット

			GF_Disp_GX_VisibleControlInit();
			GF_Disp_GXS_VisibleControlInit();
			GX_SetVisiblePlane( 0 );
			GXS_SetVisiblePlane( 0 );

			// VRAMバンク割り当て
			SetVramBank();

			// 下画面を3D面に
			sys.disp3DSW = DISP_3D_TO_SUB;
			GF_Disp_DispSelect();

			// アプリケーションメモリー
			ptr = PTFrame_AllocEventWork(work, sizeof(PKTHLN_BREAK));
			MI_CpuClear8(ptr, sizeof(PKTHLN_BREAK));

			// ヒープIDを保持
			ptr->HeapID = HEAPID_PT_BREAK;
			ptr->GameTimer = GAME_TIME;

			// 次のシーケンスへ
			PTFrame_IncSubSeq(work);
			break;
		case 1:
			// BGLアロケーション
			ptr->bgl = GF_BGL_BglIniAlloc( ptr->HeapID );

			PTFrame_SetRecBGMask(work, BG_FRM_M_RSLT_MASK );

			// BG 初期化
			BG_Init( ptr->bgl, ptr->HeapID );

			BG_BmpWinCreate( work );

			// BGリソースロード
			BG_LoadRes( ptr );

			// CATS 生成
			OBJ_Create( ptr );

			// ポケアクター初期化
			ptr->PokeActCntPtr = PokeAct_Init(
					ptr->HeapID, POKE_ACT_NUM, PRI_COMM_POKE,
					ptr->CmnResCntPtr, CATS_GetClactSetPtr(ptr->crp) );

			// 次のシーケンスへ
			PTFrame_IncSubSeq(work);
			break;
		case 2 :
			// ポケアクター セットアップ
			{
				int i,k;
				int cnt = 0;
				MONS_INFO info[ POKE_ACT_NUM ];
				PCLACT_SET_PARAM param = {0};

				for( i=0; i<ENTRY_MEMBER_NUM_MAX; i++ )
				{
					int TeamID = i;

					// 自分のポケはロードしない
					if( TeamID == PTFrameGetCurrentID(work) )
					{
						continue;
					}

					for( k=0; k<ENTRY_MAX; k++ )
					{
						int PokeID = k;

						GF_ASSERT( cnt < POKE_ACT_NUM );

						PTFrame_SetMonsInfo( work, TeamID, PokeID, &info[cnt] );
						param.ShadowPos[cnt] =
							PTFrame_GetPokeShdwPosType(PTFrame_GetPkthlnParam(work, TeamID, PokeID));

						cnt++;
					}
				}

				param.BGPriority = 1;
				param.ResVramType = NNS_G2D_VRAM_TYPE_2DMAIN;
				param.DrawArea = NNS_G2D_VRAM_TYPE_2DMAIN;

				PokeAct_SetPokeActor( ptr->PokeActCntPtr, POKE_ACT_NUM, info, &param, 0, 0 );
			}

			// 次のシーケンスへ
			PTFrame_IncSubSeq(work);
			break;
		case 3 :
			if( PokeAct_CheckActSetTask( ptr->PokeActCntPtr ) )
			{
				// CATS管理のOBJリソースをロード
				// ※汎用アクターロード後に呼ぶこと
				OBJ_LoadRes( ptr );
				OBJ_LoadResPokegra( ptr->csp, ptr->crp );

				//カウントダウンアクター 初期化
				{
					CLACT_WORK_PTR	*act_adr;
					act_adr = PTFrame_SetCountDownByClSet( work, OBJ_DISTANCE, ptr->CmnResCntPtr, CATS_GetClactSetPtr(ptr->crp) );
					//プライオリティセット
					CLACT_DrawPriorityChg( *act_adr, PRI_COUNT );
				}

				// 次のシーケンスへ
				PTFrame_IncSubSeq(work);
			}
			break;
		case 4:
			{
				// 一般アクター生成
				OBJ_CreateActor( ptr );
				// Create Kawara Controller
				ptr->cntKawara = KawaraCnt_Create( ptr->HeapID, ptr->csp, ptr->crp, work );

				// 次のシーケンスへ
				PTFrame_IncSubSeq(work);
			}
			break;
		case 5:
			{
				// Create Break Pokemon Controller
				ptr->mainPoke = BreakPoke_Create( ptr->HeapID, ptr->csp, ptr->crp, work );
				// Create Comm Poke Controller
				ptr->cntCommPoke = CommPokeCnt_Create( ptr->HeapID, ptr->csp, ptr->crp, work, ptr->PokeActCntPtr );

				// 全ポケモン停止
				PokeAct_AllAnmStop( ptr->PokeActCntPtr, TRUE );

				// サーバ初期化
				if ( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
				{
					// Create AI Controller
					ptr->cntAI = BreakAICnt_Create( work, ptr->HeapID );
				}

				// BGの初回情報を記述
				BG_PrintFirst( ptr );

				// Init Comm Disp Controller
				CommDispCnt_Init( work );

				// VBlank Set
				PTFrame_SetDefaultVBlankFunc(ptr->bgl);

				// 通信アイコンを起動
				WirelessIconEasyPokethlon( NNS_G2D_VRAM_TYPE_2DMAIN );

				//描画を許可
				PTFrame_SetDraw(work, TRUE);

				//ＯＢＪの表示
				GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// MAIN DISP OBJ ON
				GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// SUB DISP OBJ ON

				// AIから送信
				if ( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
				{
					BreakAICnt_Comm(ptr->cntAI );
				}

				// フェードイン
				WIPE_SYS_Start(	WIPE_PATTERN_WMS, WIPE_TYPE_FADEIN,
							WIPE_TYPE_FADEIN, WIPE_FADE_WHITE,
							WIPE_DEF_DIV, WIPE_DEF_SYNC, ptr->HeapID );

				// 次のシーケンスへ
				PTFrame_IncSubSeq(work);
			}
			break;
		case 6 :
			// 親機のデータをあらかじめセットしておく
			if( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
			{
				COMM_SendData(work);
			}
			COMM_Main(work);
			// 次のシーケンスへ
			PTFrame_IncSubSeq(work);
			break;
		case 7 :
			if( WIPE_SYS_EndCheck() )
			{
				HOSAKA_PRINT("BREAK InitFunc called.\n");
				return PTH_RC_END;
			}
			break;

		default : GF_ASSERT(0);
	}
	return PTH_RC_LOOP;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	描画関数
 *
 *	@param	PKTHLN_PTR work
 *	@param	pParam
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
BOOL PE_BREAK_DrawFunc( PKTHLN_PTR work, void* pParam )
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);

	CATS_Draw( ptr->crp );

	return TRUE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	競技結果計算関数
 *
 *	@param	PKTHLN_PTR work
 *	@param	pParam
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
BOOL PE_BREAK_ResultFunc( PKTHLN_PTR work, void* pParam )
{
	int i;
	RSLT_PTR pRes;

	pRes = PTFrame_GetResultPtr(work);
	RSLT_Setup( pRes );

	for( i=0; i<ENTRY_MEMBER_NUM_MAX; i++ )
	{
		int rec;

		rec = PTFrame_GetEvtResult(work, i );
		RSLT_SetResult( work, pRes, i, rec, 0, 3, msg_pkthln_rs_K4 );
	}
	// ソート
	RSLT_Sort(pRes, SORT_REC_LARGE);

	// 得点セット
	PTC_CalcPoint(pRes, PKTHLN_EVT_BREAK);

	//総得点に加算
	RSLT_AddTotalScore( pRes );

	// 競技得点セット
	RSLT_SetEvtScore( pRes, PKTHLN_EVT_BREAK );

	HOSAKA_PRINT("BREAK ResultFunc Called.\n");
	return TRUE;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	終了関数
 *
 *	@param	PKTHLN_PTR work
 *	@param	pParam
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
BOOL PE_BREAK_EndFunc( PKTHLN_PTR work, void* pParam )
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);

	CommDispCnt_Delete( ptr );

	// Delete AI Controller (親のみ)
	if ( PTFrameGetCurrentID(work) == COMM_PARENT_ID )
	{
		BreakAICnt_Delete( ptr->cntAI );
	}
	// Delete Kawara Controller
	KawaraCnt_Delete( ptr->cntKawara );
	// Delete Comm Poke Controller
	CommPokeCnt_Delete( ptr->cntCommPoke );
	// Delete Break Poke Controller
	BreakPoke_Delete( ptr->mainPoke );

	// CATS開放
	OBJ_DeleteActor( ptr );
	OBJ_Delete( ptr );

	// BG開放
	BG_Delete( ptr );

	WirelessIconEasyEnd();

	sys_VBlankFuncChange( NULL, NULL );	// VBlankセット
	sys_HBlankIntrSet( NULL,NULL );		// HBlankセット

	// イベントワーク開放
	PTFrame_FreeEventWork(work);

	// 上下画面を元に戻しておく
	sys.disp3DSW = DISP_3D_TO_MAIN;
	GF_Disp_DispSelect();

	// ヒープ開放
	sys_DeleteHeap( HEAPID_PT_BREAK );

	HOSAKA_PRINT("BREAK EndFunc Called.\n");

	return TRUE;
}


//=============================================================================
/**
 *								static関数
 */
//=============================================================================

//--------------------------------------------------------------------------------------------
/**
 * VRAM設定		ポケスロンでは、ポケモン表示だけで0x30C00必要となる。
 * VRAM_AB(0x40000)でないとはいらん
 *
 * @param	none
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void SetVramBank(void)
{
	GF_BGL_DISPVRAM tbl = {
		GX_VRAM_BG_64_E,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット

		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット

		GX_VRAM_OBJ_256_AB,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット

		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット

		GX_VRAM_TEX_NONE,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_NONE			// テクスチャパレットスロット
	};
	GF_Disp_SetBank( &tbl );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	CATSシステム生成
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_Create( PKTHLN_BREAK_PTR ptr )
{
	TCATS_RESOURCE_NUM_LIST crnl = { CHR_ID_MAX, PAL_ID_MAX, CEL_ID_MAX, ANM_ID_MAX, 0 };

	TCATS_OAM_INIT	coi = {
		0, 126,
		0, 32,
		1, 126,
		0, 32,
	};

	TCATS_CHAR_MANAGER_MAKE ccmm = {
		0,	// dummy
		1024*256,
		1024*16,
		GX_OBJVRAMMODE_CHAR_1D_256K,
		GX_OBJVRAMMODE_CHAR_1D_256K,
	};
	ccmm.num = OBJ_ID_MAX;

	ptr->csp = CATS_AllocMemory( ptr->HeapID );
	ptr->crp = CATS_ResourceCreate( ptr->csp );

	CATS_SystemInit( ptr->csp, &coi, &ccmm, 32 );
	CATS_ClactSetInit( ptr->csp, ptr->crp, CAP_ALL_NUM);
	CATS_ResourceManagerInit( ptr->csp, ptr->crp, &crnl );

	//サブサーフェースの距離を設定
	CLACT_U_SetSubSurfaceMatrix( CATS_EasyRenderGet( ptr->csp ), 0, OBJ_DISTANCE );

	ptr->CmnResCntPtr = CmnRes_Init( ptr->HeapID,
			CMN_RES_ALL_BIT|CMN_RES_PEN_M_BIT|CMN_RES_F2X2_BIT|CMN_RES_F2X4_BIT, NNS_G2D_VRAM_TYPE_2DMAIN );

	// ポケアイコン用のアーカイブハンドルOPEN
	ptr->cntCommDisp.pArcPokeIcon = ArchiveDataHandleOpen( ARC_POKEICON, ptr->HeapID );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	CATSシステム開放
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_Delete( PKTHLN_BREAK_PTR ptr )
{
	ArchiveDataHandleClose( ptr->cntCommDisp.pArcPokeIcon );

	PokeAct_Free( ptr->PokeActCntPtr );
	CmnRes_Free( ptr->CmnResCntPtr );
	CATS_ResourceDestructor_S( ptr->csp, ptr->crp );
	CATS_FreeMemory( ptr->csp );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	メインワークで管理するアクターを初期化
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_CreateActor( PKTHLN_BREAK_PTR ptr )
{
	int i;
	VecFx32 vec = {0};

	// スコアフォント
	for( i=0; i<SCORE_FIGRE; i++ )
	{
		vec.x = SCORE_FONT_POS_X + i * SCORE_FONT_OFS_X;
		vec.y = SCORE_FONT_POS_Y;

		ptr->actScore[i] = CmnRes_CreateF2X4Act( ptr->CmnResCntPtr, CATS_GetClactSetPtr(ptr->crp), 0, ptr->HeapID );
		CLACT_SetMatrix( ptr->actScore[i], &vec );
		CLACT_DrawPriorityChg( ptr->actScore[i], PRI_SCORE );
		CLACT_AnmChg( ptr->actScore[i], 1 );
		CLACT_SetDrawFlag( ptr->actScore[i], 1 );
	}

	// 時計アイコン
	ptr->cap[ CAP_ID_M_ICON_TIMER ] =
		OBJ_AddCommon(	ptr->csp, ptr->crp,
						ICON_TIME_POS_X, ICON_TIME_POS_Y,
						NANR_break_obj_main_IconTimer, PRI_SCORE );

	// 数
	for( i=0; i<TIME_FIGRE; i++ )
	{
		VecFx32 vec = {0};

		ptr->actTime[i] =
			CmnRes_CreateF2X2Act( ptr->CmnResCntPtr, CATS_GetClactSetPtr(ptr->crp), PRI_SCORE, ptr->HeapID );

		// 座標
		vec.x = ( ( ICON_TIME_POS_X + 16 + 4 ) + 16 * i ) * FX32_ONE;
		vec.y = ICON_TIME_POS_Y * FX32_ONE;
		vec.z = 0;
		CLACT_SetMatrix( ptr->actTime[i], &vec );

		CLACT_SetDrawFlag( ptr->actTime[i], 1 );
	}


	// こうたいボタン
	ptr->cap[ CAP_ID_M_BTN_CHANGE ] =
		OBJ_AddCommon( ptr->csp, ptr->crp, BTN_CHANGE_POS_X, BTN_CHANGE_POS_Y, NANR_break_obj_main_BtnChange0, PRI_SCORE );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	メインワークで管理するアクターを削除
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_DeleteActor( PKTHLN_BREAK_PTR ptr )
{
	int i;

	GF_ASSERT( ptr );

	for( i=0; i<CAP_ID_MAX; i++ )
	{
		GF_ASSERT( ptr->cap[i] );
		CATS_ActorPointerDelete_S( ptr->cap[i] );
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	OBJ用リソースのロード
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_LoadRes( PKTHLN_BREAK_PTR ptr )
{
	int i;
	CATS_SYS_PTR csp = ptr->csp;
	CATS_RES_PTR crp = ptr->crp;
	NNS_G2D_VRAM_TYPE vram_type;


	//===========================================================
	// Main OBJ
	//===========================================================
	vram_type = NNS_G2D_VRAM_TYPE_2DMAIN;

	CATS_LoadResourceCharArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_main_lz_ncgr,
			TRUE, vram_type, CHR_ID_MAIN );

	CATS_LoadResourcePlttArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_main_nclr,
			0, 2, vram_type, PAL_ID_MAIN );

	CATS_LoadResourceCellArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_main_lz_ncer,
			TRUE, CEL_ID_MAIN );

	CATS_LoadResourceCellAnmArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_main_lz_nanr,
			TRUE, ANM_ID_MAIN );

	//===========================================================
	// Sub OBJ
	//===========================================================
	vram_type = NNS_G2D_VRAM_TYPE_2DSUB;

	CATS_LoadResourceCharArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_sub_lz_ncgr,
			TRUE, vram_type, CHR_ID_SUB );

	CATS_LoadResourcePlttArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_sub_nclr,
			0, 3, vram_type, PAL_ID_SUB );

	CATS_LoadResourceCellArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_sub_lz_ncer,
			TRUE, CEL_ID_SUB );

	CATS_LoadResourceCellAnmArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_break_obj_sub_lz_nanr,
			TRUE, ANM_ID_SUB );

	// ポケアイコン パレット
	ptr->cntCommDisp.PlttOfsPokeIcon =
		CATS_LoadResourcePlttArc( csp, crp, ARC_POKEICON,
			PokeIconPalArcIndexGet(), 0, POKEICON_PAL_MAX,
			vram_type, PAL_ID_S_POKEICON );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	ポケグラ用リソースロード
 *
 *	@param	CATS_SYS_PTR csp
 *	@param	crp
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_LoadResPokegra( CATS_SYS_PTR csp, CATS_RES_PTR crp )
{
	int i;
	NNS_G2D_VRAM_TYPE vram_type;

	vram_type = NNS_G2D_VRAM_TYPE_2DMAIN;
	for( i=0; i<OBJ_POKEGRA_M_MAX; i++ )
	{
		CATS_LoadResourceCharArc(
				csp, crp, ARC_PKTHEV_BREAK,
				NARC_pkthev_break_pokegra_dmy_vram256_lz_ncgr,
				TRUE, vram_type, CHR_ID_M_POKEGRA + i );

		CATS_LoadResourcePlttArc(
				csp, crp, ARC_PKTHEV_BREAK,
				NARC_pkthev_break_pokegra_dmy_vram256_nclr,
				0, 1, vram_type, PAL_ID_M_POKEGRA + i );
	}

	vram_type = NNS_G2D_VRAM_TYPE_2DSUB;
	for( i=0; i<OBJ_POKEGRA_S_MAX; i++ )
	{
		CATS_LoadResourceCharArc(
				csp, crp, ARC_PKTHEV_BREAK,
				NARC_pkthev_break_pokegra_dmy_vram256_lz_ncgr,
				1, vram_type, CHR_ID_S_POKEGRA + i );

		CATS_LoadResourcePlttArc(
				csp, crp, ARC_PKTHEV_BREAK,
				NARC_pkthev_break_pokegra_dmy_vram256_nclr,
				0, 1, vram_type, PAL_ID_S_POKEGRA + i );
	}

	// main 256k
	CATS_LoadResourceCellArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_pokegra_dmy_vram256_lz_ncer,
			TRUE, CEL_ID_POKEGRA );

	CATS_LoadResourceCellAnmArc( csp, crp, ARC_PKTHEV_BREAK,
			NARC_pkthev_break_pokegra_dmy_vram256_lz_nanr,
			TRUE, ANM_ID_POKEGRA );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	OBJスコアフォントにスコアを反映
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_ScoreFontSet( PKTHLN_BREAK_PTR ptr )
{
	int i;
	u16 score;

	GF_ASSERT(ptr);

	score = KawaraCnt_GetBreakCount( ptr->cntKawara );

	for( i=0; i<SCORE_FIGRE; i++ )
	{
		u16 temp = score % 10;

		CLACT_AnmChg( ptr->actScore[ (SCORE_FIGRE-1)-i ], temp+1 );

		if( score < 10 )
		{ break; }
		else
		{ score /= 10; }
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	OBJ 終了演出
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void OBJ_Finish( PKTHLN_BREAK_PTR ptr )
{
	int i;

	for( i=0; i<CAP_ID_MAX; i++	)
	{
		CATS_ObjectAutoAnimeSetCap( ptr->cap[ i ], FALSE );
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BG初期化
 *
 *	@param	GF_BGL_INI* bgl
 *	@param	HeapID
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_Init( GF_BGL_INI* bgl, HEAPID HeapID )
{
	{	/* BG SYSTEM */
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BGMODE_0, GX_BG0_AS_2D,
		};
		GF_BGL_InitBG( &BGsys_data );
	}

	{
		int i;
		GF_BGL_BGCNT_HEADER bg_data[ BG_USE_NUM ] = {
			// -- MAIN --
			{	// BG_0 ベース
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x0000,GX_BG_CHARBASE_0x04000,GX_BG_EXTPLTT_01,
				3,0,0,FALSE
			},
			{	// BG_1 テキスト
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x0800,GX_BG_CHARBASE_0x08000,GX_BG_EXTPLTT_01,
				1,0,0,FALSE
			},
			{	// BG_2 リザルト
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x1000,GX_BG_CHARBASE_0x0c000,GX_BG_EXTPLTT_01,
				0,0,0,FALSE
			},
			// -- SUB --
			{	// BG_0 ベース
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x0000,GX_BG_CHARBASE_0x04000,GX_BG_EXTPLTT_01,
				3,0,0,FALSE
			},
			{	// BG_1 テキスト用ウィンドウ
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x0800,GX_BG_CHARBASE_0x0c000,GX_BG_EXTPLTT_01,
				1,0,0,FALSE
			},
			{	// BG_2 テキスト
				0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x1000,GX_BG_CHARBASE_0x14000,GX_BG_EXTPLTT_01,
				0,0,0,FALSE
			},
		};

		for( i=0; i<BG_USE_NUM; i++ )
		{
			GF_BGL_BGControlSet( bgl, c_BG_UseList[i], &(bg_data[i]), GF_BGL_MODE_TEXT );
			GF_BGL_ScrClear( bgl, c_BG_UseList[i] );
			GF_BGL_ClearCharSet( c_BG_UseList[i], 32, 0, HeapID );
		}
	}

	//タッチフォントロード
	FontProc_LoadFont(FONT_TOUCH, HeapID);

	//レコードＢＧ非表示
	GF_Disp_GX_VisibleControl( BG_FRM_M_RSLT_MASK, VISIBLE_OFF );

	GF_BGL_ScrollSet( bgl, BG_FRM_M_BASE, GF_BGL_SCROLL_Y_SET, SCR_DEF_OFS );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BG削除
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_Delete( PKTHLN_BREAK_PTR ptr )
{
	int i;

	// ニックネームメモリ開放
	for( i=0; i<ENTRY_MAX; i++ )
	{
		STRBUF_Delete( ptr->NickName[i] );
	}

	WORDSET_Delete( ptr->WordSet );
	MSGMAN_Delete( ptr->MsgMan );

	// BMPWIN 開放
	for( i=0; i<BMPWIN_ID_MAX; i++ )
	{
		GF_BGL_BmpWinDel( &ptr->win[i] );
	}

	// BGコントーラー開放
	for( i=0; i<BG_USE_NUM; i++ )
	{
		GF_BGL_BGControlExit( ptr->bgl, c_BG_UseList[i] );
	}

	//タッチフォントロード
	FontProc_UnloadFont(FONT_TOUCH);

	// BGL開放
	sys_FreeMemoryEz( ptr->bgl );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BGリソースロード
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_LoadRes( PKTHLN_BREAK_PTR ptr )
{
	// ++ 下画面 (Main) ++
	//キャラ
	ArcUtil_BgCharSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_main_lz_ncgr,
						ptr->bgl, BG_FRM_M_BASE, 0, 0, TRUE, ptr->HeapID);
	//スクリーン
	ArcUtil_ScrnSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_main_lz_nscr,
						ptr->bgl, BG_FRM_M_BASE, 0, 0, TRUE, ptr->HeapID);
	//パレット
	ArcUtil_PalSet( ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_main_nclr,
						PALTYPE_MAIN_BG, 0, 0, ptr->HeapID );

	//システム用フォントパレット転送
	SystemFontPaletteLoad(PALTYPE_MAIN_BG, MAIN_FONT_PAL*0x20, ptr->HeapID);

	// ++ 上画面 (Sub) ++

	//キャラ
	ArcUtil_BgCharSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_sub_lz_ncgr,
						ptr->bgl, BG_FRM_S_BASE, 0, 0, TRUE, ptr->HeapID);

	ArcUtil_BgCharSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_sub_lz_ncgr,
						ptr->bgl, BG_FRM_S_WIN, 0, 0, TRUE, ptr->HeapID);
	//スクリーン
	ArcUtil_ScrnSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_sub_lz_nscr,
						ptr->bgl, BG_FRM_S_BASE, 0, 0, TRUE, ptr->HeapID);

	ArcUtil_ScrnSet(ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_sub_win_lz_nscr,
						ptr->bgl, BG_FRM_S_WIN, 0, 0, TRUE, ptr->HeapID);
	//パレット
	ArcUtil_PalSet( ARC_PKTHEV_BREAK, NARC_pkthev_break_break_bg_sub_nclr,
						PALTYPE_SUB_BG, 0, 0, ptr->HeapID );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWIN関係 初期化
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_BmpWinCreate( PKTHLN_PTR work )
{
	PKTHLN_BREAK_PTR ptr;
	int i;

	ptr = PTFrame_GetEventWorkPtr(work);

	ptr->MsgMan = MSGMAN_Create(MSGMAN_TYPE_DIRECT, ARC_MSG, NARC_msg_pokethlon_dat, ptr->HeapID );
	ptr->WordSet = WORDSET_Create( ptr->HeapID );

	for( i=0; i<BMPWIN_ID_MAX; i++ )
	{
		GF_BGL_BmpWinAddEx( ptr->bgl, &ptr->win[i], &c_BmpWinList[i] );
		GF_BGL_BmpWinDataFill( &ptr->win[i], COL_NL );
		GF_BGL_BmpWinClearScrn( &ptr->win[i] );
	}

	// ニックネームメモリ確保
	// ニックネーム取得
	{
		POKETHLON_ENTRY_DATA* entry;
		entry = PTFrame_GetEntryDataPtrByID( work, PTFrameGetCurrentID(work));
		for( i=0; i<ENTRY_MAX; i++ )
		{
			ptr->NickName[i] = STRBUF_Create( MONS_NAME_SIZE + EOM_SIZE, ptr->HeapID );
			STRBUF_SetStringCode( ptr->NickName[i], entry[i].NickName );
		}
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BG テキスト更新
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_PrintFlash( PKTHLN_BREAK_PTR ptr )
{
	int temp;
	u8 sec,sec_t,sec_o;

	GF_ASSERT(ptr);
	GF_ASSERT(ptr->mainPoke);

	temp = ptr->GameTimer / GAME_FPS;

	// 念のため0以下対策
	if( temp < 0 )
	{
		temp = 0;
	}

	//秒計算
	sec = temp % 100;
	sec_t = sec / 10;
	sec_o = sec % 10;

	//秒を書き換え
	// BG表記→OBJ表記
	CLACT_AnmChg( ptr->actTime[0], sec_t + 1 );
	CLACT_AnmChg( ptr->actTime[1], sec_o + 1 );

//	WORDSET_RegisterNumber( ptr->WordSet, 0, sec, 2, NUMBER_DISPTYPE_ZERO, NUMBER_CODETYPE_DEFAULT );
//	WORDSET_RegisterNumber( ptr->WordSet, 1, ms, 2, NUMBER_DISPTYPE_ZERO, NUMBER_CODETYPE_DEFAULT );
//	BMPWIN_Print( &ptr->win[ BMPWIN_ID_M_TIME ], ptr->WordSet, ptr->MsgMan, msg_pkthln_break_time, ptr->HeapID );

	// ポケ名表示
	{
		u8 PokeIdx;

		PokeIdx = BreakPoke_GetPokeIdx( ptr->mainPoke );

		// 次のポケモンを参照
		PokeIdx = (PokeIdx+1) % ENTRY_MAX;

		WORDSET_RegisterWord( ptr->WordSet, 0, ptr->NickName[ PokeIdx ], PM_NEUTRAL, TRUE, PM_LANG );
		BMPWIN_Print( &ptr->win[ BMPWIN_ID_S_POKENICK ], ptr->WordSet, ptr->MsgMan, msg_pkthln_run_next, ptr->HeapID );
	}
}
//-----------------------------------------------------------------------------
/**
 *	@brief	BG 固定テキスト表示
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BG_PrintFirst( PKTHLN_BREAK_PTR ptr )
{
	BMPWIN_Print( &ptr->win[ BMPWIN_ID_M_SCORE_CAP ], ptr->WordSet, ptr->MsgMan, msg_pkthln_break_score_cap, ptr->HeapID );
	BG_PrintFlash( ptr );
}


//-----------------------------------------------------------------------------
/**
 *	@brief	BPMWINにテキストを描画
 *
 *	@param	GF_BGL_BMPWIN* win
 *	@param	WordSet
 *	@param	MsgMan
 *	@param	msgIdx
 *	@param	HeapID
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void BMPWIN_Print( GF_BGL_BMPWIN* win, WORDSET* WordSet, MSGDATA_MANAGER* MsgMan, u32 msgIdx, u32 HeapID )
{
	STRBUF* str;

	GF_BGL_BmpWinDataFill( win, 0 );

	str = MSGDAT_UTIL_AllocExpandString( WordSet, MsgMan, msgIdx, HeapID );
	GF_STR_PrintColor( win, FONT_SYSTEM, str, 0, 0, MSG_NO_PUT, COL_N_WHITE, NULL );
	STRBUF_Delete( str );
	GF_BGL_BmpWinOn( win );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	通信上画面管理モジュール 初期化
 *
 *	@param	PKTHLN_PTR work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void CommDispCnt_Init( PKTHLN_PTR work )
{
	int i;
	int k;
	PKTHLN_BREAK_PTR ptr;

	ptr = PTFrame_GetEventWorkPtr(work);

	GF_ASSERT(ptr);
	GF_ASSERT(ptr->csp);
	GF_ASSERT(ptr->crp);
	GF_ASSERT(ptr->cntCommPoke);

	for( i=0; i<ENTRY_MEMBER_NUM_MAX; i++ )
	{
		s16 px, py;
		COMM_DISP* disp = &ptr->cntCommDisp.team[i];

		// ポケアイコン
		px = 8 * 15 + ( 8 * 4 * i );
		py = 8 * 19;

		disp->capPoke = OBJ_AddCommonSub( ptr->csp, ptr->crp, px, py, NANR_break_obj_sub_icon0 + i , PRISUB_POKEICON );

		// キャラ転送
		CommDispCnt_SetPokeIcon( work, i, 0 );

		// ボール
		disp->capBall = OBJ_AddCommonSub( ptr->csp, ptr->crp, px, py, NANR_break_obj_sub_ball, PRISUB_POKEICON );
		CATS_ObjectEnableCap( disp->capBall, CATS_ENABLE_FALSE );

		// かわら後列
		for( k=0; k<COMM_KAWARA_BACK_MAX; k++ )
		{
			u8 pri;

			pri = PRISUB_KAWARA_BACK_HEAD + COMM_KAWARA_MAX - k;
			px = 8 * 15 + ( 8 * 4  * i );
			py = 8 * 17 - ( COMM_KAWARA_HIGHT * 4) * k;
			px += COMM_KAWARA_BACK_POS_DIFF_X;
			py += COMM_KAWARA_BACK_POS_DIFF_Y;

			disp->capKawaraBack[k] = OBJ_AddCommonSub( ptr->csp, ptr->crp, px, py, g_kawara_anmseq[i][2], pri );
			CATS_ObjectEnableCap( disp->capKawaraBack[k], CATS_ENABLE_FALSE );
		}

		// かわら前列
		for( k=0; k<COMM_KAWARA_MAX; k++ )
		{
			u8 pri;

			pri = PRISUB_KAWARA_HEAD + COMM_KAWARA_MAX - k;
			px = 8 * 15 + ( 8 * 4  * i );
			py = 8 * 17 - COMM_KAWARA_HIGHT * k;

			disp->capKawara[k] = OBJ_AddCommonSub( ptr->csp, ptr->crp, px, py, g_kawara_anmseq[i][1], pri );
			CATS_ObjectEnableCap( disp->capKawara[k], CATS_ENABLE_FALSE );
		}
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	通信上画面管理モジュール 削除
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void CommDispCnt_Delete( PKTHLN_BREAK_PTR ptr )
{
	int i;
	int k;

	for( i=0; i<ENTRY_MEMBER_NUM_MAX; i++ )
	{
		COMM_DISP* disp = &ptr->cntCommDisp.team[i];

		// ポケアイコン
		GF_ASSERT( disp->capPoke );
		CATS_ActorPointerDelete_S( disp->capPoke );

		// ボール
		GF_ASSERT( disp->capBall );
		CATS_ActorPointerDelete_S( disp->capBall );

		// かわら前列
		for( k=0; k<COMM_KAWARA_MAX; k++ )
		{
			GF_ASSERT( disp->capKawara[k] );
			CATS_ActorPointerDelete_S( disp->capKawara[k] );
		}

		// かわら後列
		for( k=0; k<COMM_KAWARA_BACK_MAX; k++ )
		{
			GF_ASSERT( disp->capKawaraBack[k] );
			CATS_ActorPointerDelete_S( disp->capKawaraBack[k] );
		}
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	通信上画面管理モジュール 情報更新
 *
 *	@param	PKTHLN_PTR work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void CommDispCnt_Set( PKTHLN_PTR work )
{
	int t;
	PKTHLN_BREAK_PTR ptr;
	COMM_EVENT_COMMON_DATA* data;
	BREAK_PARENT_DATA* parent;

	GF_ASSERT(work);

	ptr = PTFrame_GetEventWorkPtr(work);
	data = PTFrame_GetEvtComCmnData(work);
	parent = PKTHLN_CommPack_GetData(&data->ReciveShareData);

	for( t=0; t<ENTRY_MEMBER_NUM_MAX; t++ )
	{
		BREAK_COMM_POKE_PARAM* cpp;
		COMM_DISP* disp;
		int BlockNum;

		cpp = &parent->cpp[t];
		disp = &ptr->cntCommDisp.team[t];

		BlockNum = cpp->BreakCount / 5;

//		HOSAKA_PRINT("[%d] sub disp BlockNum:%d \n",t, BlockNum);

		// かわら後列表示判定
		if( BlockNum > COMM_KAWARA_MAX )
		{
			// 後列がまだ出ていなかった場合
			if( CATS_ObjectEnableGetCap( disp->capKawaraBack[0] ) == CATS_ENABLE_FALSE )
			{
				int k;

				// 後列出現
				for( k=0; k<COMM_KAWARA_BACK_MAX; k++ )
				{
					CATS_ObjectEnableCap( disp->capKawaraBack[k], CATS_ENABLE_TRUE );
				}

				// 前列クリア
				for( k=0; k<COMM_KAWARA_MAX; k++ )
				{
					CATS_ObjectEnableCap( disp->capKawara[k], CATS_ENABLE_FALSE );
				}
			}
			BlockNum -= COMM_KAWARA_MAX;
		}

		// かわら前列更新
		{
			int k;
			int front_num = BlockNum;	// 全列の数

//			HOSAKA_PRINT("[%d] fornt num :%d \n", t, front_num);

			// オーバー回避 まるめこみ
			if( front_num > COMM_KAWARA_MAX ){ front_num = COMM_KAWARA_MAX; }

			// 全ブロックチェック
			for( k=0; k < front_num; k++ )
			{
				if( CATS_ObjectEnableGetCap(disp->capKawara[k] ) == CATS_ENABLE_FALSE )
				{
					CATS_ObjectAnimeReStartCap( disp->capKawara[k] );
					CATS_ObjectEnableCap( disp->capKawara[k], CATS_ENABLE_TRUE );
				}
			}
		}



		// ポケアイコン交換判定
		if( cpp->bChange )
		{
			if( cpp->PokeIdx != disp->PokeIdxPre )
			{
				// 転送
				CommDispCnt_SetPokeIcon( work, t, cpp->PokeIdx );
				disp->PokeIdxPre = cpp->PokeIdx;
			}
			CATS_ObjectEnableCap( disp->capPoke, CATS_ENABLE_FALSE );
			CATS_ObjectEnableCap( disp->capBall, CATS_ENABLE_TRUE );
		}
		else
		{
			CATS_ObjectEnableCap( disp->capBall, CATS_ENABLE_FALSE );
			CATS_ObjectEnableCap( disp->capPoke, CATS_ENABLE_TRUE );
		}
	}

}

//-----------------------------------------------------------------------------
/**
 *	@brief	ポケアイコンを転送
 *
 *	@param	PKTHLN_PTR work
 *	@param	NetID
 *	@param	PokeIdx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void CommDispCnt_SetPokeIcon( PKTHLN_PTR work, u8 NetID, u8 PokeIdx )
{
	PKTHLN_BREAK_PTR ptr;
	COMM_DISP_CNT* cntDisp;
	COMM_DISP* disp;
	MONS_INFO info;

	ptr = PTFrame_GetEventWorkPtr(work);
	cntDisp = &ptr->cntCommDisp;
	disp = &cntDisp->team[ NetID ];

	// ポケモン情報を取得
	PTFrame_SetMonsInfo( work, NetID, PokeIdx, &info );

	// ポケアイコン転送
	CATS_PokeIconTrans( disp->capPoke, &info, cntDisp->PlttOfsPokeIcon, cntDisp->pArcPokeIcon, 0x20*4*4*NetID, ptr->HeapID );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	交代チェック
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *	@param	b_kawara_touch
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void ChangeCheck( PKTHLN_BREAK_PTR ptr, BOOL b_kawara_touch )
{
	BPOKE_STATE state;
	u32 anmseq;

	state = BreakPoke_GetState( ptr->mainPoke );

	//--------------------------------------------------------------------------
	// ペンによる交代判定
	//--------------------------------------------------------------------------
	{
		TP_CHANGE_WORK* pTpChg = &ptr->wkTpChg;

		// タッチ開始
		if( GF_TP_GetTrg() )
		{
			BOOL b_poke_touch = FALSE;
			u32 px, py;

			GF_TP_GetPointTrg( &px, &py );

			// ポケモンとの当たり判定
			b_poke_touch = ( px >= (START_POKE_MINE_POS_X - 5*8) && px <= (START_POKE_MINE_POS_X + 5*8) &&
							py >= (START_POKE_MINE_POS_Y - 5*8) && py <= (START_POKE_MINE_POS_Y + 5*8) );

			if( b_kawara_touch || b_poke_touch )
			{
				pTpChg->PosYTouch = py;
				pTpChg->PosYRelease = py;
				pTpChg->Count = 1;
				pTpChg->bTouch = 1;

				HOSAKA_PRINT("touch change check start !! \n");
			}
		}
		// タッチパネルを離した
		else if( pTpChg->bTouch && GF_TP_GetCont() == 0 )
		{
			int diff;

			diff = pTpChg->PosYTouch - pTpChg->PosYRelease;

			HOSAKA_PRINT("touch change diff:%d \n",diff);

			if( diff >= CHANGE_TP_DIFF )
			{
				// 状態チェック
				if( BreakPoke_CheckChangeEnable( ptr->mainPoke ) )
				{
//					Snd_SePlay( SEQ_SE_GS_CHANGE1 );
					// 交代処理開始
					BreakPoke_Change( ptr->mainPoke );
				}
				// 交代できない状態
				else
				{
					// 交代失敗音
//					Snd_SePlay( SEQ_SE_GS_CHANGE2 );
				}
			}

			pTpChg->bTouch = 0;
		}
		// タッチ中
		else if( pTpChg->bTouch )
		{
			u32 px, py;

			GF_TP_GetPointCont( &px, &py );

			pTpChg->PosYRelease = py;
			pTpChg->Count++;

			// 指定SYNCしか判定しない
			if( pTpChg->Count >= CHANGE_TP_SYNC )
			{
				pTpChg->bTouch = 0;
			}
		}
	}

	//--------------------------------------------------------------------------
	// ボタンによる交代判定
	//--------------------------------------------------------------------------

	// 初回入力以外、タッチをしていたら処理を飛ばす
	if( GF_TP_GetTrg() == 0 && GF_TP_GetCont() > 0 ){ return; }

	{
		static const RECT_HIT_TBL HitTbl[] = {
			{ 1*8, BTN_CHANGE_SIZE_Y, 0, BTN_CHANGE_SIZE_X, },
			{ RECT_HIT_END, 0, 0, 0, },
		};

		if( GF_TP_RectHitTrg( HitTbl ) != RECT_HIT_NONE )
		{
			if( BreakPoke_CheckChangeEnable( ptr->mainPoke ) )
			{
				// ボタン押した
				anmseq = NANR_break_obj_main_BtnChange1;
				Snd_SePlay( SEQ_SE_GS_CHANGE1 );

				// 交代処理開始
				BreakPoke_Change( ptr->mainPoke );
			}
			else
			{
				// 交代できないときにボタンを押した
				anmseq = NANR_break_obj_main_BtnChange3;
				Snd_SePlay( SEQ_SE_GS_CHANGE2 );
			}
		}
		//　ボタン押してない
		else
		{
			if( BreakPoke_CheckChangeEnable( ptr->mainPoke ) == FALSE )
			{
				// 交代不能状態
				anmseq = NANR_break_obj_main_BtnChange2;
			}
			else if( BreakPoke_GetSweatFlag( ptr->mainPoke ) )
			{
				// 交代促し
				anmseq = NANR_break_obj_main_BtnChange4;
			}
			else
			{
				// 通常状態
				anmseq = NANR_break_obj_main_BtnChange0;
			}
		}

		CATS_ObjectAnimeSeqCheckSetCap( ptr->cap[ CAP_ID_M_BTN_CHANGE ], anmseq );
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	自機データを親機に送信
 *
 *	@param	PKTHLN_PTR work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void COMM_SendData( PKTHLN_PTR work )
{
	PKTHLN_BREAK_PTR ptr = PTFrame_GetEventWorkPtr(work);
	COMM_EVENT_COMMON_DATA* data = PTFrame_GetEvtComCmnData(work);
	BREAK_CHILD_DATA* child = PKTHLN_CommPack_GetData(&data->SendData);
	const BREAK_POKE_PARAM* bpp = BreakPoke_GetActiveParam( ptr->mainPoke );
	BPOKE_STATE state = BreakPoke_GetState( ptr->mainPoke );

	child->BreakCount = KawaraCnt_GetBreakCount( ptr->cntKawara );
	child->Stamina = bpp->Stamina;
	child->Tension = BreakPoke_GetTension( ptr->mainPoke );
	child->bHighTension = BreakPoke_GetHighTensionFlag( ptr->mainPoke );
	child->bChange = ( state == BPOKE_STATE_CHANGE );
	child->PokeIdx = BreakPoke_GetPokeIdx( ptr->mainPoke );

//	HOSAKA_PRINT("send my stamina:%d \n", bpp->Stamina);
}

//-----------------------------------------------------------------------------
/**
 *	@brief	通信処理
 *
 *	@param	PKTHLN_PTR work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void COMM_Main( PKTHLN_PTR work )
{
	int i;
	int CurrentID;
	PKTHLN_BREAK_PTR ptr;
	BREAK_CHILD_DATA child_data;
	COMM_EVENT_COMMON_DATA* data;

	CurrentID = PTFrameGetCurrentID(work);
	ptr = PTFrame_GetEventWorkPtr(work);
	data = PTFrame_GetEvtComCmnData(work);

	GF_ASSERT( ptr );
	GF_ASSERT( ptr->mainPoke );
	GF_ASSERT( ptr->cntKawara );

	// 親処理
	if( CurrentID == COMM_PARENT_ID )
	{
		BREAK_CHILD_DATA* child;
		BREAK_PARENT_DATA* parent;

		parent = PKTHLN_CommPack_GetData(&data->SendShareData);

		// 親のデータは直接レシーバーにセット
		{
			BREAK_CHILD_DATA* child_rec;
			child_rec = PKTHLN_CommPack_GetData(&data->ReciveData[COMM_PARENT_ID]);
			child = PKTHLN_CommPack_GetData(&data->SendData);
			(*child_rec) = (*child);
		}

		// ReciveDataを集計 > SendShareData
		for( i=0; i<ENTRY_MEMBER_NUM_MAX; i++ )
		{
			child = PKTHLN_CommPack_GetData(&data->ReciveData[i]);

			parent->cpp[i].BreakCount = child->BreakCount;
			parent->cpp[i].Stamina = child->Stamina;
			parent->cpp[i].Tension = child->Tension;
			parent->cpp[i].PokeIdx = child->PokeIdx;
			parent->cpp[i].bHighTension = child->bHighTension;
			parent->cpp[i].bChange = child->bChange;

//			HOSAKA_PRINT("[%d]recive stamina:%d \n", i, child->Stamina);
		}
	}

	// 自機データを親機に送信
	COMM_SendData( work );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	レコード表示
 *
 *	@param	work
 *	@param	inPoint
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void SetRecordStr( PKTHLN_PTR work, const int inPoint )
{
	PKTHLN_BREAK_PTR ptr;
	WORDSET *word;
	MSGDATA_MANAGER *msgman;
	STRBUF * str;
	int integer;

	ptr = PTFrame_GetEventWorkPtr(work);
    word = ptr->WordSet;
	msgman = ptr->MsgMan;
	integer = inPoint;	//整数部分

	// フレームワークに送信
	PTFrame_SetCheckRecord( work, integer );

	GF_BGL_BmpWinDataFill( &ptr->win[ BMPWIN_ID_M_RECORD ], 0);

	WORDSET_RegisterNumber(word, 0, integer, 3, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT);

    str = MSGDAT_UTIL_AllocExpandString(word, msgman, msg_pkthln_new_record04, ptr->HeapID);

	GF_STR_PrintColor( &ptr->win[ BMPWIN_ID_M_RECORD ], FONT_TOUCH, str, 0, 0, MSG_NO_PUT, COL_N_WHITE, NULL );

	STRBUF_Delete( str );//<一括表示なので、メモリをここで消してもOK

	GF_BGL_BmpWinOn( &ptr->win[ BMPWIN_ID_M_RECORD ] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	画面揺らしワーク
 *
 *	@param	TCB_PTR tcb
 *	@param	work
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void TCB_ShakeTask( TCB_PTR tcb, void* work )
{
	PKTHLN_BREAK_PTR ptr = work;
	int vec = ptr->ShakeVec ? 1 : -1;
	int Pos = ptr->ShakeVal * vec;

	GF_BGL_ScrollReq( ptr->bgl, BG_FRM_M_BASE, GF_BGL_SCROLL_Y_SET, Pos );
	GF_BGL_ScrollReq( ptr->bgl, BG_FRM_M_TEXT, GF_BGL_SCROLL_Y_SET, Pos );
	CLACT_U_SetMainSurfaceMatrix( CATS_EasyRenderGet( ptr->csp ), 0, -1*Pos*FX32_ONE );

	ptr->ShakeVal -= ptr->ShakeDecVal;
	ptr->ShakeVec ^= 1;

	if( ptr->ShakeVal <= 0 )
	{
		GF_BGL_ScrollReq( ptr->bgl, BG_FRM_M_BASE, GF_BGL_SCROLL_Y_SET, SCR_DEF_OFS );
		GF_BGL_ScrollReq( ptr->bgl, BG_FRM_M_TEXT, GF_BGL_SCROLL_Y_SET, 0 );
		CLACT_U_SetMainSurfaceMatrix( CATS_EasyRenderGet( ptr->csp ), 0, 0 );

		HOSAKA_PRINT("TCB SHAKE END\n");
		ptr->tcbShake = NULL;
		TCB_Delete( tcb );
	}

}

//-----------------------------------------------------------------------------
/**
 *	@brief	画面揺らしTCB登録関数
 *
 *	@param	PKTHLN_BREAK_PTR ptr
 *	@param	ShakeVal
 *	@param	ShakeDecVal
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void StartShakeTask( PKTHLN_BREAK_PTR ptr, u8 ShakeVal, u8 ShakeDecVal )
{
	// TCB強制終了
	if( ptr->tcbShake )
	{
		HOSAKA_PRINT("クリティカルTCB強制終了\n");
		TCB_Delete( ptr->tcbShake );
	}

	HOSAKA_PRINT("TCB SHAKE START\n");

	ptr->ShakeVec = 0;
	ptr->ShakeVal = ShakeVal;
	ptr->ShakeDecVal = ShakeDecVal;
	ptr->tcbShake = TCB_Add( TCB_ShakeTask, ptr, 2 );
}
